#include "DHT11.h"
#include "tim.h"


void Delay_us(uint16_t delay)
{
  __HAL_TIM_DISABLE(&htim3);
  __HAL_TIM_SET_COUNTER(&htim3,0);
  __HAL_TIM_ENABLE(&htim3);
  uint16_t curCnt=0;
  while(1)
  {
    curCnt=__HAL_TIM_GET_COUNTER(&htim3);
    if(curCnt>=delay)
      break;
  }
  __HAL_TIM_DISABLE(&htim3);
}
 
void TIM3_Delay_us(uint16_t n_us)
{
  __HAL_TIM_SetCounter(&htim3, 0);
  __HAL_TIM_ENABLE(&htim3);
  
  while(__HAL_TIM_GetCounter(&htim3) < ((1 * n_us)-1));
  __HAL_TIM_DISABLE(&htim3);
}

void DHT11_OUT(void)
{
  GPIO_InitTypeDef  GPIO_InitStruct = {0};
 
  GPIO_InitStruct.Pin = DHT11_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
 
void DHT11_IN(void)
{
  GPIO_InitTypeDef  GPIO_InitStruct = {0};
 
  GPIO_InitStruct.Pin  = DHT11_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
 
 
void DHT11_Strat(void)
{
  DHT11_OUT();
  DHT11_LOW;
  HAL_Delay(20);
  DHT11_HIGH;
  Delay_us(30);
}
 
 
uint8_t DHT11_Check(void)
{
  uint8_t retry = 0 ;
  DHT11_IN();
    
  while(DHT11_IO_IN && retry <100) 
  {
    retry++;
    Delay_us(1);//1us
  }
  if(retry>=100) 
  {return  1;}
   else retry =  0 ;
    
  while(!DHT11_IO_IN && retry<100)
  {
    retry++;
    Delay_us(1);//1us
  }
    
  if(retry>=100)
  {return 1;}
  return 0 ;
}
 
 
uint8_t DHT11_Read_Bit(void)
{
  uint8_t retry = 0 ;
  while(DHT11_IO_IN && retry <100)
  {
    retry++;
    Delay_us(1);
  }
    retry = 0 ;
  while(!DHT11_IO_IN && retry<100)
  {
    retry++;
    Delay_us(1);
  }
  Delay_us(40);
  if(DHT11_IO_IN) return 1;
  else 
  return 0 ;
}
 
 
uint8_t DHT11_Read_Byte(void)
{
  uint8_t i , dat ;
  dat = 0 ;
  for(i=0; i<8; i++)
  {
    dat <<= 1;
    dat |= DHT11_Read_Bit();
  }
  return dat ; 
}
 
   
uint8_t DHT11_Read_Data(uint8_t* temp , uint8_t* humi)
{
  uint8_t buf[5];
  uint8_t i;
  DHT11_Strat();
  if(DHT11_Check() == 0)
  {
    for(i=0; i<5; i++)
    {
      buf[i] = DHT11_Read_Byte();
    }
    if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4])
    {
      *humi = buf[0];
      *temp = buf[2];
    }
  }else return 1;

  return 0;
}


